package com.api.custom.deleteRight;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.api.formmode.cache.ModeComInfo;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.docs.docs.DocComInfo;
import weaver.general.BaseBean;
import weaver.hrm.User;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author	Huangwm
 * @date  	2019年11月21日 下午3:18:46
 * @project	E9_Project
 * @name	deleteRight
 * @package com.api.custom.deleteRight
 * @description	拱北海关：删除巡察组成员所相关的流程、流程创建的文档、模块数据、模块创建的文档等涉及的数据权限
 */
@Path("/custom")
public class delCustomRight{
	
	public static Log log = LogFactory.getLog(delCustomRight.class);
	/**
	 * 巡察组角色id
	 */
	public static String roleid = "26";
	/**
	 * 巡察组虚拟监控人员id 用于更换删除权限的流程节点操作者
	 */
	public static String monitorUserid = "14266";
	/**
	 * 插入文档共享信息中的记录 文档id  角色id
	 */
	public static String insertDocShareFromRole1 = "insert into docshare(docid,sharetype,seclevel,rolelevel,sharelevel,userid,subcompanyid,departmentid,roleid,foralluser,crmid,sharesource,isSecDefaultShare,orgGroupId,downloadlevel,allmanagers,includesub,orgid,seclevelmax,joblevel,jobdepartment,jobsubcompany,jobids)"
												+"values(?,4,0,2,1,0,0,0,?,0,0,0,1,0,1,0,0,0,100,0,0,0,0)";
	/**
	 * 插入文档共享权限 文档id 角色id+0 角色id
	 */
	public static String insertDocShareFromRole2 = "insert into shareinnerdoc(sourceid,type,content,seclevel,sharelevel,srcfrom,opuser,sharesource,downloadlevel,seclevelmax,joblevel,jobdepartment,jobsubcompany)"
											    +"values(?,4,?,0,1,4,?,0,1,100,0,0,0)";
	/**
	 * 更新巡察项目信息表的是否是收回权限字段
	 */
	public static String isDel = "Status";
	
	@SuppressWarnings("finally")
	@POST
	@Path("/delCustomRight")
	@Produces(MediaType.TEXT_PLAIN)
	public static String deleteRightApi(@Context HttpServletRequest request,@Context HttpServletResponse response){
		String sourceid = request.getParameter("sourceid");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("weaver_user@bean");
		log.info("当前请求API的用户id："+user.getUID());
		JSONObject result = new JSONObject();
		//在执行以下删除的操作前,判断下当前用户是否是创建人
		//RecordSet rs = new RecordSet();
		
		boolean deleteRightToWFdoc = false;
		boolean deleteRightToWF = false;
		boolean deleteRightToModuledoc = false;
		boolean deleteRightToModule = false;
		try{
			
			JSONArray getToWFDocid = getToWFDocid(sourceid);
			log.info("getToWFDocid:"+getToWFDocid);
			JSONArray getToModuleDocid = getToModuleDocid(sourceid);
			log.info("getToModuleDocid:"+getToModuleDocid);
			JSONArray getTableDataid = getTableDataid(sourceid);
			log.info("getTableDataid:"+getTableDataid);
			
			deleteRightToWFdoc = deleteRightToWFdoc(sourceid);
			//如果删除流程关联的文档权限成功,则插入权限
			if(deleteRightToWFdoc){
				insertRightTodocLog(getToWFDocid);
			}
			
			deleteRightToWF = deleteRightToWF(getToWFDocid,sourceid);
			
			//如果删除模块关联的文档权限成功,则插入权限
			deleteRightToModuledoc = deleteRightToModuledoc(sourceid);
			if(deleteRightToModuledoc){
				insertRightTodocLog(getToModuleDocid);
			}
			
			deleteRightToModule = deleteRightToModule(getTableDataid,sourceid);
			
			result.put("deleteRightToWFdoc", deleteRightToWFdoc);
			result.put("deleteRightToWF", deleteRightToWF);
			result.put("deleteRightToModuledoc", deleteRightToModuledoc);
			result.put("deleteRightToModule", deleteRightToModule);
			
		}catch (Exception e){
			e.printStackTrace();
			result.put("deleteRightToWFdoc", deleteRightToWFdoc);
			result.put("deleteRightToWF", deleteRightToWF);
			result.put("deleteRightToModuledoc", deleteRightToModuledoc);
			result.put("deleteRightToModule", deleteRightToModule);
		}finally{
			return result.toString();
		}
	}
	
	@POST
	@Path("/updateThis")
	@Produces(MediaType.TEXT_PLAIN)
	public static String updateIsdel(@Context HttpServletRequest request,@Context HttpServletResponse response){
		String sourceid = request.getParameter("sourceid");
		JSONObject object = new JSONObject();
		try {
			RecordSet rs = new RecordSet();
			JSONObject getProjectField = getProjectField();
			String tablename = getProjectField.getString("tablename");
			String sql = "update "+tablename +" set "+isDel+"=0 where id="+sourceid;
			boolean result = rs.execute(sql);
			object.put("result", result);
			log.info("更新巡察项目收回权限字段为已收回成功");
			return object.toString();
		} catch (Exception e) {
			e.printStackTrace();
			object.put("result", false);
			log.info("更新巡察项目收回权限字段为已收回失败");
			return object.toString();
		}
		
	}
	
	/**
	 * 根据巡察项目的数据id获取与流程相关的文档id 包含表单附件上传字段和签字意见
	 * @param sourceid 巡察项目信息模块的数据id
	 * @return JSONArray 关联巡察项目数据id的流程id，对应的流程相关的文档id和附件文档id数组
	 * @throws Exception 
	 */
	public static JSONArray getToWFDocid(String sourceid) throws Exception{
		//Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
		JSONArray result = new JSONArray();
		JSONArray WfFieldDocids = new JSONArray();
		JSONArray RequestLogDocids = new JSONArray();
		
		//先获取流程表单字段的附件文档id集合
		JSONArray WfFields = getWfField();
		RecordSet rs1 = null;
		RecordSet rs2 = null;
		for(int i=0;i<WfFields.size();i++){
			JSONObject thisFields = WfFields.getJSONObject(i);
			String tablename = thisFields.getString("tablename");
			String fields = thisFields.getString("fjzd");//有可能有多个字段
			String sourcefield = thisFields.getString("source");
			//log.info("tablename="+tablename+" fields="+fields+" sourcefield="+sourcefield);
			rs1 = new RecordSet();
			String getWfFieldDocids = "select requestid,"+fields+" from "+tablename+" where "+sourcefield+"="+sourceid;
			//log.info("查询流程表单相关联的附件上传字段>> "+getWfFieldDocids);
			rs1.execute(getWfFieldDocids);
			while(rs1.next()){
				JSONObject object = new JSONObject();
				List<String> docidsList = new ArrayList<String>();
				//循环字段 获取附件字段的值先放到集合中
				for(int j=0; j<fields.split(",").length; j++) {		
					//log.info("总的有"+fields.split(",").length+"次 循环第"+j+"次 fieldname>>"+fields.split(",")[j]);
					String docid = rs1.getString(fields.split(",")[j]);
					if(!"".equals(docid)&&!"null".equals(docid)){
						docidsList.add(docid);
					}					
				}
				object.put("requestid", rs1.getString("requestid"));
				object.put("docids", docidsList.toString().replace("[", "").replace("]", "").replace(" ", ""));
				WfFieldDocids.add(object);
				result.add(object);
			}
			
			//获取相关流程签字意见中可能关联的附件
			String getRequestLogDocids = "select "+tablename+".requestid,annexdocids,"+sourcefield+" from workflow_requestLog right join  "+tablename+" on  workflow_requestLog.requestid="+
										tablename+".requestId where "+sourcefield+"="+sourceid;
			//log.info("查询流程签字意见相关联的附件>> "+getRequestLogDocids);
			rs2 = new RecordSet();
			rs2.execute(getRequestLogDocids);
			while(rs2.next()){
				JSONObject object = new JSONObject();
				List<String> docidsList = new ArrayList<String>();
				String docid = rs2.getString("annexdocids");
				if(!"".equals(docid)&&!"null".equals(docid)){
					docidsList.add(docid);
					for(int j=0;j<WfFieldDocids.size();j++){
						JSONObject thisobj = WfFieldDocids.getJSONObject(j);
						//log.error(thisobj);
						String thisRid = thisobj.getString("requestid");
						if(thisRid.equals(rs2.getString("requestid"))){
							//则表示该流程id在表单里有附件，需要先把原本的docids值取出来跟当前获取到的文档id集合
							//log.info("requestid"+rs2.getString("requestid")+"存在object中");
							String thisdocid = thisobj.getString("docids");
							docidsList.add(thisdocid);		
							object.put("requestid", rs2.getString("requestid"));
							object.put("docids", docidsList.toString().replace("[", "").replace("]", "").replace(" ", ""));
						}else {
							//该流程id不存
							object.put("requestid", rs2.getString("requestid"));
							object.put("docids", docidsList.toString().replace("[", "").replace("]", "").replace(" ", ""));
						}
					}		
					RequestLogDocids.add(object);
					result.add(object);
				}		
			}	
		}
		//log.info("流程表单附件字段文档id>>"+WfFieldDocids);
		//log.info("流程签字意见附件字段文档id>>"+RequestLogDocids);
		//log.info("getToWFDocid结果>>"+result);
		return result;
	}
	
	/**
	 * 删除获取到的流程关联的文档权限
	 * @return boolean
	 */
	public static boolean deleteRightToWFdoc(String sourceid) throws Exception{
		log.info("删除来源于流程的文档权限开始");
		JSONArray WFDocid = getToWFDocid(sourceid);
		if(WFDocid.size()>0){
			RecordSet rs1 = new RecordSet();
			RecordSet rs2 = new RecordSet();
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < WFDocid.size(); i++){
				JSONObject object = WFDocid.getJSONObject(i);
				String docids = object.getString("docids");
				if(!"".equals(docids)){
					sb.append(docids+",");
				}
			}
			//需要判断下  如果没有文档直接返回true
			if(!"".equals(sb)&&sb.length()>0){
				String arrayDocids = sb.substring(0,sb.length()-1);
				if(!"".equals(arrayDocids)&&arrayDocids.length()>1){
					Object[] objects = delArrayDeduplication(arrayDocids.split(","));
					String docids = getArrayDeduplicatio(objects);
					log.info("删除来源于流程的文档权限时获取的文档id："+docids.toString());
					String delete_docshare = "delete from docshare where docid in("+docids.toString()+")";
					String delete_shareinnerdoc = "delete from shareinnerdoc where sourceid in("+docids.toString()+")";
					boolean delete1 = rs1.execute(delete_docshare);
					boolean delete2 = rs2.execute(delete_shareinnerdoc);
					log.info("delete_docshare: "+delete_docshare);
					log.info("delete_shareinnerdoc: "+delete_shareinnerdoc);
					if(delete1 && delete2){
						DocComInfo docComInfo = new DocComInfo();
						docComInfo.removeDocCache();
						docComInfo.removeCache();
						log.info("删除来源于流程的文档权限成功结束");
						return true;
					}else {
						log.info("删除来源于流程的文档权限失败结束");
						return false;
					}
				}else{
					log.info("未找到来源于流程的文档");
					return true;
				}
			}else{
				log.info("未找到来源于流程的文档");
				return true;
			}
		}else{
			log.info("未找到来源于流程的文档");
			return true;
		}		
	}
	
	/**
	 * 插入文档共享记录--指定角色
	 * @return boolean
	 * @throws Exception 
	 */
	public static void insertRightTodocLog(JSONArray WFDocid) throws Exception{
		log.info("插入文档权限开始");
		if(WFDocid.size()>0){
			RecordSetTrans rst1 = new RecordSetTrans();
			RecordSetTrans rst2 = new RecordSetTrans();
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < WFDocid.size(); i++){
				JSONObject object = WFDocid.getJSONObject(i);
				String docids = object.getString("docids");
				if(!"".equals(docids)){
					sb.append(docids+",");
				}
			}
			if(!"".equals(sb)&&sb.length()>0){
				String arrayDocids = sb.substring(0,sb.length()-1);
				if(!"".equals(arrayDocids)&&arrayDocids.length()>1){
					Object[] objects = delArrayDeduplication(arrayDocids.split(","));
					String docids = getArrayDeduplicatio(objects);
					log.info("插入文档共享权限时获取的文档id："+docids);
					for(int j = 0; j < objects.length; j++){
						rst1 = new RecordSetTrans();
						rst2 = new RecordSetTrans();
						rst1.setAutoCommit(false);
						rst2.setAutoCommit(false);
						
						//文档id 角色id
						boolean r1 = rst1.executeUpdate(insertDocShareFromRole1,objects[j],roleid);
						//文档id 角色id+2(表示总部权限) 角色id
						boolean r2 = rst2.executeUpdate(insertDocShareFromRole2,objects[j],roleid+"2",roleid);
						boolean c1 = rst1.commit();
						boolean c2 = rst2.commit();
						log.info("插入文档共享信息中的记录结果 文档id="+objects[j]+" insert结果="+r1+" 是否成功insert="+c1);
						log.info("插入文档共享权限录结果 文档id="+objects[j]+" insert结果="+r2+" 是否成功insert="+c2);
					}
					DocComInfo docComInfo = new DocComInfo();
					docComInfo.removeDocCache();
					docComInfo.removeCache();
					log.info("插入文档权限成功结束");
				}else{
					log.info("没有需要插入权限的文档");
				}
			}else{
				log.info("没有需要插入权限的文档");
			}
		}else{
			log.info("没有需要插入权限的文档");
		}	
	}
	
	/**
	 * 删除获取到的流程查看权限
	 * @return boolean
	 */
	public static boolean deleteRightToWF(JSONArray getToWFDocid,String sourceid) throws Exception{
		log.info("清除流程共享权限开始");
		if(getToWFDocid.size()>0){
			RecordSet rs = null;
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < getToWFDocid.size(); i++){
				JSONObject object = getToWFDocid.getJSONObject(i);
				String requestid = object.getString("requestid");
				if(!"".equals(requestid)){
					sb.append(requestid+",");
				}
			}
			//需要判断下  如果没有文档直接返回true
			String arrRequestids = sb.substring(0,sb.length()-1);
			if(!"".equals(arrRequestids)&&arrRequestids.length()>1){
				rs = new RecordSet();
				Object[] objects = delArrayDeduplication(arrRequestids.split(","));
				String requestids = getArrayDeduplicatio(objects);
				log.info("删除流程共享权限时获取的流程id："+requestids);
				ArrayList<String> userids = getProjectMembers(sourceid);
				ArrayList<String> roleUserids = getHrmRoleMembers();
				//去掉重复的,即去掉项目组成员里包含的巡察组成员角色id
				userids.removeAll(roleUserids);
				String updateUserids = userids.toString().replace("[", "").replace("]", "").replace(" ", "");
				log.info("删除流程共享权限时获取的巡察组项目内成员id："+updateUserids);
				//不进行删除,因为删除后流程相当于回到了发起节点
				//String sql = "delete from workflow_currentoperator where requestid in("+requestids+") and userid in("+userids+")";
				String UpdateWc = "update  workflow_currentoperator set userid="+monitorUserid+" where requestid in("+requestids+") and userid in("+updateUserids+")";
				log.info("UpdateWc: "+UpdateWc);
				boolean result = rs.execute(UpdateWc);
				if(result){
					log.info("清除流程共享权限成功结束");
					return true;
				}else{
					log.info("清除流程共享权限失败");
					return false;
				}			
			}else{
				log.info("没有需要清除权限的流程");
				return true;
			}
		}else {
			log.info("没有需要清除权限的流程");
			return true;
		}		
	}
	
	/**
	 * 根据巡察项目的数据id获取相关的文档id
	 * 例如各个关联模块有上传附件，需要进行查询各模块的附件对应文档id
	 * @param sourceid
	 * @return Map<Integer, List<Integer>>
	 */
	public static JSONArray getToModuleDocid(String sourceid) throws Exception{
		JSONArray result = new JSONArray();
		JSONArray FieldDocids = new JSONArray();
		//先获取建模表单字段的附件文档id集合
		JSONArray ModuleFields = getModuleField();
		RecordSet rs1 = null;
		for(int i=0;i<ModuleFields.size();i++){
			JSONObject object = new JSONObject();
			JSONObject thisFields = ModuleFields.getJSONObject(i);
			String tablename = thisFields.getString("tablename");
			String fields = thisFields.getString("fjzd");//有可能有多个字段
			String sourcefield = thisFields.getString("source");
			rs1 = new RecordSet();
			String sql = "select id,"+fields+","+sourcefield+" from "+tablename+" where "+sourcefield+"="+sourceid;
			rs1.execute(sql);
			while(rs1.next()){	
				List<String> docidsList = new ArrayList<String>();
				//循环字段 获取附件字段的值先放到集合中
				for(int j=0; j<fields.split(",").length; j++) {		
					String docid = rs1.getString(fields.split(",")[j]);
					if(!"".equals(docid)&&!"null".equals(docid)){
						docidsList.add(docid);
					}					
				}
				object.put("id", rs1.getString("id"));
				object.put("sourceid", sourceid);
				object.put("docids", docidsList.toString().replace("[", "").replace("]", "").replace(" ", ""));
				FieldDocids.add(object);
				result.add(object);
			}
		}
		return result;
	}
	
	/**
	 * 删除获取到的模块关联的文档权限
	 * @return boolean
	 */
	public static boolean deleteRightToModuledoc(String sourceid) throws Exception{
		log.info("删除来源于建模文档权限开始");
		JSONArray ModuleDocid = getToModuleDocid(sourceid);
		if(ModuleDocid.size()>0){
			RecordSet rs1 = new RecordSet();
			RecordSet rs2 = new RecordSet();
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < ModuleDocid.size(); i++){
				JSONObject object = ModuleDocid.getJSONObject(i);
				String docids = object.getString("docids");
				if(!"".equals(docids)){
					sb.append(docids+",");
				}
			}
			if(!"".equals(sb)&&sb.length()>0){
				//需要判断下  如果没有文档直接返回true
				String arrayDocids = sb.substring(0,sb.length()-1);
				if(!"".equals(arrayDocids)&&arrayDocids.length()>1){
					Object[] objects = delArrayDeduplication(arrayDocids.split(","));
					String docids = getArrayDeduplicatio(objects);
					log.info("删除来源于建模文档权限时获取的文档id："+docids.toString());
					String delete_docshare = "delete from docshare where docid in("+docids.toString()+")";
					String delete_shareinnerdoc = "delete from shareinnerdoc where sourceid in("+docids.toString()+")";
					boolean delete1 = rs1.execute(delete_docshare);
					boolean delete2 = rs2.execute(delete_shareinnerdoc);
					log.info("delete_docshare: "+delete_docshare);
					log.info("delete_shareinnerdoc: "+delete_shareinnerdoc);
					if(delete1 && delete2){
						DocComInfo docComInfo = new DocComInfo();
						docComInfo.removeDocCache();
						docComInfo.removeCache();
						log.info("删除来源于建模文档权限成功结束");
						return true;
					}else {
						log.info("删除来源于建模文档权限失败结束");
						return false;
					}
				}else{
					log.info("未找到来源于建模的文档");
					return true;
				}
			}else{
				log.info("未找到来源于建模的文档");
				return true;
			}
		}else{
			log.info("未找到来源于建模的文档");
			return true;
		}		
	}
	
	/**
	 * 获取巡察项目所涉及的建模表的关联数据id
	 * @param sourceid
	 * @return ArrayList<String>
	 * @throws Exception
	 */
	public static JSONArray getTableDataid(String sourceid) throws Exception{	
		JSONArray result = new JSONArray();
		JSONArray table = getTable();
		RecordSet rs1 = null;;
		JSONObject getProjectField = getProjectField();
		JSONObject object1 = new JSONObject();
		object1.put("tablename", getProjectField.getString("tablename"));
		object1.put("formmodeid", getProjectField.getString("formmodeid"));
		object1.put("dataids", sourceid);
		result.add(object1);
		
		for(int i=0;i<table.size();i++){
			JSONObject object2 = new JSONObject();
			ArrayList<String> dataidList = new ArrayList<String>();
			JSONObject thisObject = table.getJSONObject(i);
			String tablename = thisObject.getString("tablename");
			String formmodeid = thisObject.getString("formmodeid");
			String sourceField = thisObject.getString("source");
			String sql = "select id from "+tablename+" where "+sourceField+"="+sourceid;
			rs1 = new RecordSet();
			rs1.execute(sql);
			while(rs1.next()){			
				dataidList.add(rs1.getString("id"));
			}
			object2.put("tablename", tablename);
			object2.put("formmodeid", formmodeid);
			object2.put("dataids", dataidList.toString().replace("[", "").replace("]", "").replace(" ", ""));
			result.add(object2);
		}
		return result;
	}
	
	/**
	 * 删除巡察项目所关联的各子模块的数据权限
	 * @return boolean
	 */
	public static boolean deleteRightToModule(JSONArray getTableDataid,String sourceid) throws Exception{
		log.info("删除巡察项目所关联的各子模块的数据权限开始");
		boolean result = true;
		if(getTableDataid.size()>0){
			RecordSet rs1 = null;
			RecordSet rs2 = null;
			ArrayList<String> userids = getProjectMembers(sourceid);
			ArrayList<String> roleUserids = getHrmRoleMembers();
			//去掉重复的,即去掉项目组成员里包含的巡察组成员角色id
			userids.removeAll(roleUserids);
			String delUserids = userids.toString().replace("[", "").replace("]", "").replace(" ", "");
			if(!"".equals(delUserids)&&delUserids.length()>1){
				log.info("删除巡察项目所关联的各子模块的数据时获取的用户id："+delUserids);
				for(int i = 0; i < getTableDataid.size(); i++){
					JSONObject thisObject = getTableDataid.getJSONObject(i);
					//String tablename = thisObject.getString("tablename");
					String thisDataid = thisObject.getString("dataids");
					String formmodeid = thisObject.getString("formmodeid");
					if(!"".equals(thisDataid)){
						rs1 = new RecordSet();
						rs2 = new RecordSet();
						String sql1 = "delete from Modedatashare_"+formmodeid+" where  sourceid in("+thisDataid+") and content in("+delUserids+")";
						boolean delResult1 = rs1.execute(sql1);
						String sql2 = "delete from Modedatashare_"+formmodeid+"_set where  sourceid in("+thisDataid+") and relatedid in("+delUserids+")";
						boolean delResult2 = rs2.execute(sql2);
						if(!delResult1){
							log.info("删除Modedatashare_"+formmodeid+"的数据权限失败");
							log.info("删除语句："+sql1);
							result =  false;
							break;
						}else if(!delResult2) {
							log.info("删除Modedatashare_"+formmodeid+"_set的数据权限失败");
							log.info("删除语句："+sql2);
							result =  false;
							break;
						}else{
							log.info("删除Modedatashare_"+formmodeid+":"+sql1);
							log.info("删除Modedatashare_"+formmodeid+"_set:"+sql2);				
							log.info("删除formmodeid为"+formmodeid+"的数据id["+thisDataid+"]的权限成功");					
						}
					}
				}
			}else{
				log.info("没有需要删除的各模块数据权限");
				return result;
			}
		}else{
			log.info("没有需要删除的各模块数据权限");
			return result;
		}
		if(result){	
			log.info("删除巡察项目所关联的各子模块的数据权限成功");
		}
		ModeComInfo modeComInfo = new ModeComInfo();
		modeComInfo.removeCache();
		return result;
	}
	
	/**
	 * 获取配置文件里配置好的流程表名和字段名
	 * @return JSONArray
	 */
	public static JSONArray getWfField() throws Exception{
		log.info(new BaseBean().getPropValue("table_field","workflow"));
		JSONArray WfFields = JSONArray.fromObject(new BaseBean().getPropValue("table_field","workflow"));
		return WfFields;
	}
	
	/**
	 * 获取配置文件里配置好的建模表名和字段名
	 * @return JSONArray
	 */
	public static JSONArray getModuleField() throws Exception{
		log.info(new BaseBean().getPropValue("table_field","module"));
		JSONArray ModuleFields = JSONArray.fromObject(new BaseBean().getPropValue("table_field","module"));
		return ModuleFields;
	}
	
	/**
	 * 获取配置文件里配置好的巡察组成员字段
	 * @return JSONObject
	 */
	public static JSONObject getProjectField() throws Exception{
		JSONObject ProjectFields = JSONObject.fromObject(new BaseBean().getPropValue("table_field","projetField"));
		return ProjectFields;
	}
	
	/**
	 * 获取配置文件里配置好的建模关联表单
	 * @return JSONObject
	 */
	public static JSONArray getTable() throws Exception{
		JSONArray tables = JSONArray.fromObject(new BaseBean().getPropValue("table_field","othertable"));
		return tables;
	}
	
	
	/**
	 * 获取角色里的人员
	 * @return ArrayList<Integer>
	 */
	public static ArrayList<String> getHrmRoleMembers() throws Exception{
		RecordSet rs1 = new RecordSet();
		RecordSet rs2 = new RecordSet();
		ArrayList<String> list = new ArrayList<String>();
		String sql1 = "select resourceid,workcode,lastname from  HrmRoleMembers left join HrmResource on HrmRoleMembers.resourceid=HrmResource.id where  roleid="+roleid+" and HrmRoleMembers.resourcetype=1";
		rs1.execute(sql1);
		while(rs1.next()){
			String resourceid = rs1.getString("resourceid");
			if(!"".equals(resourceid)&&!"null".equals(resourceid )){	
				list.add(resourceid);
			}	
		}
		
		String sql2 = "select id,workcode,lastname from HrmResource where departmentid in(select resourceid from HrmRoleMembers where roleid="+roleid+" and resourcetype=3)";
		rs2.execute(sql2);
		while(rs2.next()){
			String resourceid = rs1.getString("id");
			if(!"".equals(resourceid)&&!"null".equals(resourceid)){
				list.add(resourceid);
			}	
		}
		return list;
	}
	
	/**
	 * 根据巡察项目id获取对应的组成员
	 * @return ArrayList<String>
	 */
	public static ArrayList<String> getProjectMembers(String sourceid) throws Exception{
		ArrayList<String> list = new ArrayList<String>();
		JSONObject PeojectFields = getProjectField();
		String tablename = PeojectFields.getString("tablename");
		String fields = PeojectFields.getString("field");
		RecordSet rs = new RecordSet();
		String sql = "select "+fields+" from "+tablename+" where id="+sourceid;
		rs.execute(sql);
		if(rs.next()){
			for(int i = 0; i < fields.split(",").length; i++){
				//log.info(fields.split(",")[i]+">>"+rs.getString(fields.split(",")[i]));
				list.add(rs.getString(fields.split(",")[i]));
			}
		}
		return list;
	}
	
	/**
	 * 去除数组中相同值的元素
	 * @param Object[] 
	 * @return Object[]
	 */
	public static Object[] delArrayDeduplication(Object[] originArray) {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < originArray.length; i++) {
            if (!list.contains(originArray[i])) {
                list.add(originArray[i]);
            }
        }
        Object[] resultArray = list.toArray();
        return resultArray;
    }
	
	/**
	 * 获取去重后的数组字符串
	 * @param Object[]
	 * @return String
	 */
	public static String getArrayDeduplicatio(Object[] originArray) { 
        Object[] resultArray = delArrayDeduplication(originArray);
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < resultArray.length; i++) {
			list.add(resultArray[i].toString());
		}
        return list.toString().replace("[", "").replace("]", "").replace(" ", "");
    }

}
